#include <Wire.h>  // For IIC

#include <Arduino.h>
#include <ArduinoJson.h>

#include <Adafruit_SHT31.h> //For SHT31

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h> //For SSD1306

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "mqtt.h"

#include "images.h" // images

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
Adafruit_SHT31 sht31 = Adafruit_SHT31();

WiFiClient esp8266Client;
PubSubClient client(esp8266Client);

bool wifi_status = false;
bool mqtt_status = false;

void initWiFi() {
  int sleeptime = 0;

  display.clearDisplay();
  display.setTextSize(1);
  WiFi.mode(WIFI_STA);
  WiFi.begin(WiFi_SSID, WiFi_Password);
  // Wait for connection
  Serial.print("Connecting to ");

  Serial.println(WiFi_SSID);


  display.setCursor(0, 0);
    display.println("WiFi:");
    display.setCursor(28, 0);
    display.println(String(WiFi_SSID));
    display.display();
  

  while (WiFi.status() != WL_CONNECTED) {
    sleeptime += 1;
    delay(500);
    Serial.print(".");
    if (sleeptime > 6) {
      wifi_status = false;
      break;
    }
  }
  Serial.println("");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());


  if (WiFi.status() == WL_CONNECTED) {

    display.setCursor(0, 10);
    display.println("Connected");
    Serial.println("WiFi Connected");
    wifi_status = true;
  }
  else if (wifi_status == false) {
    display.setCursor(0, 10);
    display.println("Failed");
    Serial.println("WIFI Failed");
  }

  display.display();

  delay(1000);
}

void initMQTT() {
  int sleeptime = 0;

  display.setCursor(0, 20);
  display.println("MQTT Server");
  client.setServer(mqtt_server, mqtt_port);

  Serial.println("Connecting to MQTT Server");
  Serial.println(mqtt_server);

  display.setCursor(0, 30);
  display.println(String(mqtt_server));

  display.display();

  while (!client.connected()) {
    sleeptime ++;
    client.connect(ClientId, Username, Password);
    delay(10);
    Serial.print(".");
    if (sleeptime >= 3 ) {
      break;
    }
  }
  Serial.println("");
  delay(100);
  display.setCursor(0, 40);
  if (client.connected()) {
    display.println("Connected");
    mqtt_status = true;
    Serial.println("Connected");
  }
  else {
    display.println("Failed");
    Serial.println("Failed");
  }

  display.display();

  delay(3500);
}

void MQTT_Post(float Msg1, float Msg2, String deviceID) {
  StaticJsonDocument<256> JsonSHT31_data;
  JsonSHT31_data["Ctemp"] = Msg1;
  JsonSHT31_data["Humid"] = Msg2;
  JsonSHT31_data["deviceID"] = deviceID;
  String JsonSHT31;
  serializeJson(JsonSHT31_data, JsonSHT31);
  if (client.connected()){
  client.publish(report_Topic, JsonSHT31.c_str());
  Serial.print(report_Topic); Serial.print("MSG:"); Serial.println(JsonSHT31);
  Serial.println("\n");
  Serial.println("MQTT Publish OK!");
  }
  else{
  Serial.println("MQTT Publish Failed!");
  display.clearDisplay();
  display.setCursor(0, 0);
  display.println("MQTT Publish");
  display.setCursor(16, 0);
  display.println("Failed!!!");
  display.display();
  delay(1000);
  }
}

void firstPage() {
  display.drawBitmap(0, 16, E_LOGO, 32, 32, 1);
  display.drawBitmap(32, 16, ES_LOGO, 32, 32 , 1);
  display.drawBitmap(64, 16, Q_LOGO, 32, 32, 1);
  display.drawBitmap(96, 16, QS_LOGO, 32, 32, 1);
}

void initDisp_TR() {

  // check SSD1306 display && SHT3X == IIC address

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
  }
  if (! sht31.begin(0x44)) {
    Serial.println("Couldn't find SHT31");
  } // check

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  display.setTextSize(2);

  firstPage();

  display.display();
  delay(1000);
}

void OLED(float Temp, float humid) {
  display.setTextSize(2);
  display.drawBitmap(2, 8, wen, 20, 20, 1);
  display.drawBitmap(22, 8, du, 20, 20, 1);
  display.drawBitmap(40, 8, maohao, 20, 20, 1);
  display.setCursor(50, 15);
  //    display.println("24.73");
  display.println(String(Temp));
  display.drawBitmap(108, 8, sheshidu, 20, 20, 1);

  display.drawBitmap(2, 38, shi, 20, 20, 1);
  display.drawBitmap(22, 38, du, 20, 20, 1);
  display.drawBitmap(40, 38, maohao, 20, 20, 1);
  display.setCursor(50, 45);
  //    display.println("88.88");
  display.println(String(humid));
  display.drawBitmap(108, 38, baifenhao, 20, 20, 1);
  //    display.drawBitmap(32,0,manman,64,64,1); //Show 馒馒
  display.display();
}

void setup()
{
  Serial.begin(115200);

  Serial.println("Display&IIC");
  initDisp_TR();


  Serial.println("WIFI");
  initWiFi();

  Serial.println("MQTT");
  if (wifi_status == true) {
    initMQTT();
  }

  Serial.println("Now Start loop()");
}

void loop() {
  display.clearDisplay();
  float cTemp = sht31.readTemperature();
  float humidity = sht31.readHumidity();

  OLED(cTemp, humidity);
  if (mqtt_status==true) {
    MQTT_Post(cTemp, humidity, devID);
  }

  delay(10000);
}
